<?php
class EmbedController extends CpController{
    public function init(){        
        $url = new Url();
        $this->breadcrumbs[] = array('name'=>'Quản trị mã nhúng','link'=>$url->createUrl("embed/index"),'class'=>'');
    }
    public function actionIndex(){
        $this->breadcrumbs[] = array('name'=>'Danh sách mã nhúng','link'=>"#",'class'=>'active');
        $row_per_page = 10;
        $page = isset($_GET["page"]) ? intval($_GET["page"]):1;
        list($max_page,$total,$embed) = AEmbed::getDataByAdmin($page,$row_per_page);
        $embedIds = array();
        foreach($embed as $value){
            $embedIds[] = $value["id"];
        }
        $embed_detail = AEmbed::getDataByEmbedIds($embedIds);

        for($i=0;$i<count($embed);$i++){
            foreach($embed_detail as $key=>$value){
                if($embed[$i]["id"]==$key){
                    $embed[$i]["data"] = $value["data"]; 
                }
            } 
        }
        $this->render("index",array("embed"=>$embed,"max_page"=>$max_page,"total"=>$total,"page"=>$page));
    }

    public function actionCreate(){
        $this->breadcrumbs[] = array('name'=>'Thêm mới mã nhúng','link'=>'#','class'=>'active');
        $this->render("create");
    }

    public function actionEdit(){
        $id = isset($_GET["id"]) ? intval($_GET["id"]):0;
        $this->breadcrumbs[] = array('name'=>'Sửa mã nhúng','link'=>'#','class'=>'active');
        $embed = AEmbed::getDataById($id);
        if($embed){
            $game_ids = "";
            $embed["type_display"] = 0;
            $embed_detail = AEmbed::getDataByEmbedId($id);
            foreach($embed_detail as $key=>$value){
                $game_ids .=  $value["dataId"].',';
                $embed["type_display"] = $value["type_display"];
            }
            $embed["game_ids"] = $game_ids;
            $this->render("edit",array("embed"=>$embed));
        }
    }

    public function actionAjaxSaveEmbed(){
        $username = Yii::app()->user->name;
        $title = isset($_POST["title_embed"]) ? trim(strip_tags($_POST["title_embed"])):"";
        $game_ids = isset($_POST["game_ids"]) ? trim($_POST["game_ids"],","):"";
        $type_display = isset($_POST["type_display"]) ? intval($_POST["type_display"]):0;
        $status = isset($_POST["status"]) ? intval($_POST["status"]):0;
        if(Common::generate_slug($title)==''){
            echo "Tên mã nhúng không hợp lệ";exit;
        }
        if($game_ids==""){
            echo "Chưa nhập ID game để nhúng";exit;
        } 
        $layout = json_encode(LoadConfig::$type_display[$type_display]);

        $data_code = array(
        "user_id"=>array("value"=>0,"type"=>1),
        "title"=>array("value"=>$title,"type"=>2),
        "status"=>array("value"=>$status,"type"=>1),
        "create_date"=>array("value"=>time(),"type"=>1),
        "create_user"=>array("value"=>$username,"type"=>2),
        );
        $embed_id = CommonDB::insertRow("embed_data",$data_code);
        if($embed_id >0){
            $arr_game = explode(",",$game_ids);
            for($i=0;$i<count($arr_game);$i++){
                $arr_game[$i] = intval($arr_game[$i]);
            }
            $data_game = AGame::getDataByArrId($arr_game);
            $arr_key = array("embed_id","dataId","catId","type_cat","type_display","layout","create_date","create_user");
            $items = $_SESSION["items"];
            foreach($data_game as $key=>$value){
                $arr_values[] = array($embed_id,$value["id"],$value["categoryId"],1,$type_display,$layout,time(),$username);

            }
            $result = CommonDB::insertManyRows("embed_data_detail",$arr_key,$arr_values);
            $act = 1;
            $obj = 26;
            $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$embed_id.',name: '.$title.',game_ids: '.$game_ids.'}';
            $result = AAdminLog::insertAdminLog($act,$obj,$content);
            echo 1;exit;  
        }else{
            echo "Chưa thay đổi dữ liệu"; 
        }

    }

    public function actionAjaxUpdateEmbed(){
        $username = Yii::app()->user->name;
        $title = isset($_POST["title_embed"]) ? trim(strip_tags($_POST["title_embed"])):"";
        $game_ids = isset($_POST["game_ids"]) ? trim($_POST["game_ids"],","):"";
        $type_display = isset($_POST["type_display"]) ? intval($_POST["type_display"]):0;
        $status = isset($_POST["status"]) ? intval($_POST["status"]):0;
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        if(Common::generate_slug($title)==''){
            echo "Tên mã nhúng không hợp lệ";exit;
        }
        if($game_ids==""){
            echo "Chưa nhập ID game để nhúng";exit;
        } 
        $data_old = AEmbed::getDataById($id);
        if(!$data_old){
            echo "Không tồn tại mã nhúng này";exit;
        }

        $data_code = array(
        "user_id"=>array("value"=>0,"type"=>1),
        "title"=>array("value"=>$title,"type"=>2),
        "status"=>array("value"=>$status,"type"=>1),
        "id"=>array("value"=>$id,"type"=>1),
        );
        $result = CommonDB::updateRow("embed_data",$data_code,array("id"));
        $result = CommonDB::deleteRow("embed_data_detail",array("embed_id"=>$id)); 
        $layout = json_encode(LoadConfig::$type_display[$type_display]);
        $arr_game = explode(",",$game_ids);
        for($i=0;$i<count($arr_game);$i++){
            $arr_game[$i] = intval($arr_game[$i]);
        }
        $data_game = AGame::getDataByArrId($arr_game);
        $arr_key = array("embed_id","dataId","catId","type_cat","type_display","layout","create_date","create_user");
        $items = $_SESSION["items"];
        foreach($data_game as $key=>$value){
            $arr_values[] = array($id,$value["id"],$value["categoryId"],1,$type_display,$layout,time(),$username);

        }
        $result = CommonDB::insertManyRows("embed_data_detail",$arr_key,$arr_values);
        $act = 2;
        $obj = 26;
        $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',name: '.$data_old["title"].',status: '.$data_old["status"].'} thành';
        $content .= ' {id: '.$id.',name: '.$data_old["title"].',status: '.$status.',game_ids: '.$game_ids.'}';
        $result = AAdminLog::insertAdminLog($act,$obj,$content);
        echo 1;exit();  
    }

    public function actionAjaxQuickUpdate(){
        $time_now = time();
        $status = isset($_POST["status"]) ? intval($_POST["status"]):0;
        $order_embed = isset($_POST["order_embed"]) ? intval($_POST["order_embed"]):0;
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        if($id >0){
            $data_old = AEmbed::getDataById($id);
            $data = array(
            "order_embed"=>array("value"=>$order_embed,"type"=>1),
            "status"=>array("value"=>$status,"type"=>1),
            "id"=>array("value"=>$id,"type"=>1)
            );

            $table = "embed_data";
            $result = CommonDB::updateRow($table,$data,array("id"));
            if($result >0){
                $act = 2;
                $obj = 26;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',name: '.$data_old["title"].',status: '.$data_old["status"].',order_embed: '.$data_old["order_embed"].'} thành';
                $content .= ' {id: '.$id.',name: '.$data_old["title"].',status: '.$status.',order_embed: '.$order_embed.'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
    public function actionAjaxDelete(){
        $time_now = time();
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        if($id >0){
            $data_old = AEmbed::getDataById($id);
            $result = CommonDB::deleteRow("embed_data_detail",array("embed_id"=>$id));
            $result = CommonDB::deleteRow("embed_data",array("id"=>$id));

            if($result >0){
                $act = 3;
                $obj = 26;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',name: '.$data_old["title"].'}';                
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
}